//占
//---------------------------------------------------------------------------------
function myset(k, v)
{
	layui.vs[k] = v;
	return layui.vs;
}
//---------------------------------------------------------------------------------

function myget(k)
{
	return empty(k) ? layui.vs : layui.vs[k];
}


//占
//---------------------------------------------------------------------------------
function _only()
{
	return new Date().getTime();
}

function only()
{
	return new Date().getTime();
}
//---------------------------------------------------------------------------------
function jsond(data)
{
	return JSON.parse(data);
}
//---------------------------------------------------------------------------------

function jsone(data)
{
	return JSON.stringify(data);
}

//占
//---------------------------------------------------------------------------------
function globalInit()
{
	var $ = layui.$;
  $(document).keypress(function(e){
  	$('[keycode'+e.keyCode+']').trigger('click');
  });
  
    //更换图形验证码
  $(document).on('click', '#LAY-user-get-vercode', function(){
    this.src = api('piccode')+'&'+only();
  });
  
  timer_heart();
}
//---------------------------------------------------------------------------------
function upimg(id, imgid, valid)
{
	var $ = layui.$
	,upload = layui.upload;
	
	
  var uploadInst1 = upload.render({
    elem: '#'+id
    ,url: api('uploadimg')
    ,before: function(obj){

      obj.preview(function(index, file, result){
        $('#'+imgid).attr('src', result); //图片链接（base64）
      });
    }
    ,done: function(res){
      //如果上传失败
      if(res.code > 0){
        return layer.msg('上传失败');
      }
      else
      {
      	$('#'+valid).val(res.data.id);
      }
    }
  });
  
  return uploadInst1;
}
//---------------------------------------------------------------------------------
function mysql2a(rows, k, v)
{
	var arr = {};
	for(var i in rows)
	{
		var key = rows[i][k];
		var value = rows[i][v];
		arr[key] = value;
	}
	
	return arr;
}
//---------------------------------------------------------------------------------

function print(v)
{
	console.log(v);
}
//---------------------------------------------------------------------------------
function dlfile(url, params)
{
	var $ = layui.$;
	var form = $("<form></form>").attr("action", url).attr("method", "post");
	for(var i in params)
	{
		form.append($("<input></input>").attr("type", "hidden").attr("name", i).attr("value", params[i]));
	}
	form.appendTo('body').submit().remove();
}
//---------------------------------------------------------------------------------

function table_exportfile(data)
{	
	var route = layui.router().path[1];
	var pkey = myget('fieldkey');
	var params = {key:route,ids:[]};
	for(var i in data)
	{
		if(data[i].LAY_CHECKED)
		{
			params.ids.push(data[i][pkey]);
		}
	}
	
	dlfile(api('admintabledl'), params);
}
//--------------------------------------------------------------------------
function getTokenValue()
{
	var setter = layui.setter;
	var request = setter.request;
	return layui.data(setter.tableName)[request.tokenName];
}
//--------------------------------------------------------------------------
function getCheckids(res)
{
			var ids = [];
			for(var i in res.data)
			{
				ids.push(res.data[i].id);
			}
			
			return ids;
}
//--------------------------------------------------------------------------
function langTo(v)
{
	if(empty(langs[v]))
		return v;
	
	return langs[v];
}
//--------------------------------------------------------------------------

function array_add(arr1, arr2)
{
	var tmp = [];
	for(var i in arr1)
	{
		tmp.push(arr1[i]);
	}
	
	for(var j in arr2)
	{
		tmp.push(arr2[j]);
	}
	return tmp;
}

//占
//---------------------------------------------------------------------------------

function listen_set(name, k)
{
	lsval[name][k] = null;
	lsval[name][k] = true;
}
//---------------------------------------------------------------------------------

function listen_init(name, func, num)
{
	lsval[name] = [];
	var i = 0;
	for(;i<num;i++)
	{
		lsval[name][i] = false;
	}
	
	lsfunc[name] = func;
}
//---------------------------------------------------------------------------------

function listen(name)
{
	var run = true;
	
	if(!empty(lsval[name]) && lsval[name].length > 0)
	{
		for(var i in lsval[name])
		{
			if(lsval[name][i] === false)
			{
				run = false;
				break;
			}
		}
		
		if(run)
		{
			lsval[name] = [];
			lsfunc[name]();
		}
	}
}
//---------------------------------------------------------------------------------
let timeListen = {};
let lsval = {},lsfunc = {};
timer_listen();


//占
function table_init(fieldkey, params, cond, url)
{
	if(empty(fieldkey))
		fieldkey = 'id';
	
	if(empty(params))
		params = {};

	if(empty(cond))
		cond = {};

	if(empty(url))
		url = [cc,ca];

	var w = window.innerWidth;
	var h = window.innerHeight;
	var w2,h2;
	
	if(w > h)
	{
		w2 = w * 0.6;
		h2 = w * 0.4;
	}
	else
	{
		w2 = w * 0.8;
		h2 = h * 0.8;
	}
	
	myset('tableopenwindow_w', w2);
	myset('tableopenwindow_h', h2);
	
	
	myset('tableurlparams', params);
	myset('tableurls', tlbase(url));
	myset('tablelist', tlist());
	myset('tableone', tone());
	
	
	myset('fieldkey', fieldkey);
	myset('data', []);
	myset('count', 0);
	myset('cond', cond);
	myset('cols', []);
	myset('layerIndex', 0);
	myset('tableonedata', {});
	
	myset('table_tdclick', null);
	myset('table_render_after', null);
	
	myset('hastool', false);
		
	for(var i in layui.router().search)
	{
		if(strpos(i, 'condstr_') === 0)
		{			
			var tmp = substr(i, 8);
			table_condset(tmp, layui.router().search[i]);
		}
	}
	
	listen_init('main', table_render, 2);
	layui.form.render();
	
	table_flush();
}

//---------------------------------------------------------------------------------
function table_whpop(width, height)
{
	myset('tableopenwindow_w', width);
	myset('tableopenwindow_h', height);
}
//---------------------------------------------------------------------------------
function table_wh(width, height)
{
	myset('tablewidth', width);
	myset('tableheight', height);
}
//---------------------------------------------------------------------------------
function table_dblclick(v)
{
	//myset('tabledblclick', v);
}
//---------------------------------------------------------------------------------

function table_condset(k, v)
{
	var arr = myget('cond');
	if(empty(arr))
		arr = {};
	
	if(empty(k))
	{
		v = layui.$.extend(arr, v);
		myset('cond', v);
	}
	else
	{
		arr[k] = v;
		myset('cond', arr);
	}
}
//---------------------------------------------------------------------------------

function table_getcurr()
{
	var arr = myget('cond');
	if(empty(arr))
	{
		return [1, 10];
	}
	
	var page = arr.page > 1 ? arr.page : 1;
	var limit = arr.limit >= 1 ? arr.limit : 10;
	return [page,limit];
}
//---------------------------------------------------------------------------------
function table_lasttd(tableid)
{
	//放开每列最后一个td
	var head = $('#'+tableid).next().find('.layui-table-box .layui-table-header');
	var main = $('#'+tableid).next().find('.layui-table-box .layui-table-body');

	main.find('.layui-table tbody tr').each(function(){
		$('td:last div', this).css('overflow', 'visible');
	});
}
//---------------------------------------------------------------------------------
function table_actionrender(tableid, pageid, hastool, cols, data, page, limit, count, width, height)
{
			
			var tmp = {
	      elem: '#'+tableid
	      ,data: data
	      ,cellMinWidth: 100
	      ,cols: [cols]
	      ,autoSort:false
	      ,limit:limit
	      ,done:function(res, curr, count){
					table_lasttd(tableid);
	      	table_after();
	      }
	    };
			
			if(!empty(width))
			{
				tmp.width = width;
			}

			if(!empty(height))
			{
				tmp.height = height;
			}

			if(cols[0].type == 'checkbox' || hastool)
			{
				tmp.toolbar = '#test-table-toolbar-toolbarDemo';
				
				if(cols[0].type == 'checkbox')
					tmp.defaultToolbar = ['exports'];
				else
					tmp.defaultToolbar = [];
			}
						
	    layui.table.render(tmp);
			
	    //page
	    layui.laypage.render({
	      elem: pageid
	      ,count: count
	      ,curr:page
	      ,limit:limit
	      ,layout: ['prev', 'page', 'next', 'skip', 'count']
	      ,jump: function(obj, first){
	        table_condset('page', obj.curr);
	        
	        if(!first)
	        	table_flush();
	      }
	    });
}
//---------------------------------------------------------------------------------

function table_render()
{	
		layui.use(['table','laypage'], function(){

			var tmp1 = table_getcurr();
			var page = tmp1[0];
			var limit = tmp1[1];
			var cols = myget('cols');
			var data = myget('data');
			var tableid = 'test-table-simple';
			var pageid = 'test-laypage-demo7';
			var count = myget('count');
			var hastool = myget('hastool');
			var width = myget('tablewidth');
			var height = myget('tableheight');
			
			table_actionrender(tableid, pageid, hastool, cols, data, page, limit, count, width, height);
		});
}
//---------------------------------------------------------------------------------
function table_del(obj)
{
			var key = myget('fieldkey');
			var params = {dit:'del',dittype:'del'};
			params[key] = obj.data[key];
		  posturl(myget('tableone'), params, function(){
		  	table_flush();
		  });
}
//---------------------------------------------------------------------------------

function table_listen(edit, add, other, tool, seeit, tdedit)
{
		if(empty(edit))
			edit = function(){};

		if(empty(seeit))
			seeit = function(){};

		if(empty(add))
			add = function(){};

		if(empty(tdedit))
			tdedit = function(){};

		if(empty(other))
			other = function(){};

		if(empty(tool))
		{
			tool = function(){};
		}
		else
		{
			myset('hastool', true);
		}

		layui.use(['table'], function(){
			
			if(!tdclick)
			{
				tdclick = 1;
				//监听单元格编辑
				$(document).on('click', '.layui-table-cell', function(e){
					var func = myget('table_tdclick');
					if(!empty(func))
					{
						func(this, e);
					}
				});
			}
			
			layui.table.on('edit(test-table-simple)', function(obj){
				posturl(myget('tableone'), {dit:'tdedit',id:obj.data.id,key:obj.field,value:obj.value});
				tdedit(obj);
			});

	    //头工具栏事件
	    layui.table.on('toolbar(test-table-simple)', function(obj){
	      var checkStatus = layui.table.checkStatus(obj.config.id);
	      tool(obj,checkStatus);
	    });

		  //监听行工具事件
		  layui.table.on('tool(test-table-simple)', function(obj){
		  			  	
		    var data = obj.data;
		    if(obj.event === 'del')
		    {
		      layer.confirm(langbe('sure_del'), function(index){
		        
		        table_del(obj);
		        layer.close(index);
		      });
		    }
		    else if(obj.event === 'edit')
		    {
		    	edit(obj);
		    }
		    else if(obj.event === 'seeit')
		    {
		    	seeit(obj);
		    }
		    else
		    {
		    	other(obj);
		    }
		  });
			
			//添加按钮有时候不在表格里
			layui.$('#tableAdd').click(function(){
				add();
			});
		
			//监听排序
			layui.table.on('sort(test-table-simple)', function(obj){
				
				table_condset('_sort1', obj.field);
				table_condset('_sort2', obj.type == 'desc' ? 1 : 0);
				
				table_flush();
			});
			
			layui.table.on('row(test-table-simple)', function(obj){
				table_click1(obj);
			});
			 
			//监听行双击事件
			layui.table.on('rowDouble(test-table-simple)', function(obj){
				table_click2(obj);
			});
			
		});
}
//---------------------------------------------------------------------------------

function table_head()
{
	var tlll = myget('tablelist');
	var cols = myget('cols');	
	if(!empty(cols))
	{
		listen_set('main', 0);
	}
	else
	{
		posturl(tlll, {_ahead:1}, function(res){
			myset('cols', res.data);
			listen_set('main', 0);
			table_head_after();
		});
	}
}
//---------------------------------------------------------------------------------

function table_flush(done)
{
	var index = layer.load();
	var tlll = myget('tablelist');
	
	if(empty(done))
		done = function(){};
	
	var data = myget('cond');
	data._adata = 1;
	
	layui.$.ajax({
		url:tlll
    ,type: 'post'
    ,data:data
    ,dataType: 'json'
    ,success:function(res){
			myset('data', res.data.rows);
			myset('count', res.data.count);
			myset('cond', res.data.cond);
			table_head();
			listen_set('main', 1);
			layer.close(index);
			done();
    }
	});
}
//---------------------------------------------------------------------------------

function table_add(title){
	var tpl = tonelocal();
	
	return function(){
		var params = myget('tableurlparams');
		params._tpltype = 'add';
    getlocalWithParams(tpl, params, function(str){
    		//需延时执行
    	  var layerIndex = layerOpenFn(str, title, function(){ }, function(){ }, 'openwindow1', myget('tableopenwindow_w'), myget('tableopenwindow_h'));
    	  myset('layerIndex', layerIndex);
    });
  }
}
//---------------------------------------------------------------------------------

function table_edit(title)
{
	var laytpl = layui.laytpl
	,$ = layui.$;
		
	var tpl = tonelocal();
	
	return function(obj){

		var params = myget('tableurlparams');
		var key = myget('fieldkey');
		if(empty(params))
			params = {};
		
		params[key] = obj.data[key];
		params._tpltype = 'edit';
    getlocalWithParams(tpl, params, function(str){
    	  var layerIndex = layerOpenFn(str, title, function(){ }, function(){ }, 'openwindow1', myget('tableopenwindow_w'), myget('tableopenwindow_h'));
    	  myset('layerIndex', layerIndex);
    });
  }
}
//---------------------------------------------------------------------------------

function table_seeit(title)
{
	var laytpl = layui.laytpl
	,$ = layui.$;
		
	var tpl = tonelocal();

	return function(obj){
		
		var params = myget('tableurlparams');
		var key = myget('fieldkey');
		if(empty(params))
			params = {};
		
		params[key] = obj.data[key];
		params._tpltype = 'seeit';
    getlocalWithParams(tpl, params, function(str){
    	  var layerIndex = layerOpenFn(str, title, function(){ }, function(){ }, 'openwindow1', myget('tableopenwindow_w'), myget('tableopenwindow_h'));
    	  myset('layerIndex', layerIndex);
    });
  }
}
//---------------------------------------------------------------------------------
//注意，此函数只能运行一次
function table_tdlong()
{
	var stat = myget('v_global_tdlong_do_one');
	
	if(stat !== true)
	{
		myset('v_global_tdlong_do_one', true);
		_table_tdlong1();
	}
	
	_table_tdlong2();
}
//---------------------------------------------------------------------------------

function _table_tdlong1()
{
	var $ = layui.$;

	$(document).on('click', '.lgtdopen', function(){
				
		var icon = $(this).children(':eq(0)');
		var o = $(this).parent();
		var td = $(o).parent();
		var index = td.data('field');
		var len = td.siblings().length;
		var classname = 'myw60';
				
		if(index == len)
		{			
			if(o.hasClass(classname))	//收
			{
				td.attr('colspan', 1);
				td.siblings().show();
				o.removeClass(classname);
				icon.addClass('layui-icon-left');
				icon.removeClass('layui-icon-right');
			}
			else	//张
			{
				td.siblings().hide();
				td.siblings(':eq(0)').show();
				td.attr('colspan', len);
				o.addClass(classname);
				icon.removeClass('layui-icon-left');
				icon.addClass('layui-icon-right');
			}
		}
	});
}
//---------------------------------------------------------------------------------

function _table_tdlong2()
{
	
	var $ = layui.$;
	
	var newfunc = null;
	var func = myget('table_render_after');
	if(empty(func))
	{
		newfunc = function(){
			$("div[lay-id='test-table-simple'] .layui-table-box .layui-table-body table tr").each(function(){
				var o = $(this).children(':last').children(':eq(0)');
				if(!o.is('.laytable-cell-checkbox'))
				{
					o.prepend('<a class="layui-btn layui-btn-xs layui-btn-warm lgtdopen"><i class="layui-icon layui-icon-left"></i></a>');
				}
			});
		};
	}
	else
	{
		newfunc = function(){
			$("div[lay-id='test-table-simple'] .layui-table-box .layui-table-body table tr").each(function(){
				var o = $(this).children(':last').children(':eq(0)');
				if(!o.is('.laytable-cell-checkbox'))
				{
					o.prepend('<a class="layui-btn layui-btn-xs layui-btn-warm lgtdopen"><i class="layui-icon layui-icon-left"></i></a>');
				}
			});
			func();
		};
	}
	
	myset('table_render_after', newfunc);
}
//---------------------------------------------------------------------------------

function table_after()
{
	var func = myget('table_render_after');
		
	if(!empty(func))
	{
		func();
	}
}
//---------------------------------------------------------------------------------

function table_head_after()
{
	var func = myget('table_after_head');
		
	if(!empty(func))
	{
		func();
	}
}
//---------------------------------------------------------------------------------

function table_click1(obj)
{	
	var func = myget('table_click1');
		
	if(!empty(func))
	{
		func(obj);
	}
}
//---------------------------------------------------------------------------------

function table_click2(obj)
{
	var func = myget('table_click2');
		
	if(!empty(func))
	{
		func(obj);
	}
}

//占
//用数组填充表格
function tableext_renderList(cols, tableid, data, pageid, count, currpage, limit, thejump)
{	
		layui.use(['table','laypage'], function(){
						
			var tmp = {
	      elem: '#'+tableid
	      ,data: data
	      ,cellMinWidth: 80
	      ,cols: [cols]
	      ,autoSort:false
	      ,limit:limit
	      ,done:function(res, curr, count){

	      }
	    };
						
	    layui.table.render(tmp);
			
			//page
			//ID无#号
			var tmp1 = {
	      elem: pageid
	      ,count: count
	      ,curr:currpage
	      ,limit:limit
	      ,layout: ['prev', 'page', 'next', 'skip']
	      ,jump: function(obj, first){

	        if(!first)
	        {
	        	thejump(obj.curr);
	        }
	      }
	    };
	    
	    layui.laypage.render(tmp1);
			
		});
}

//用url填充表格
function tableext_listAction(url, params, cols, tabldid, pageid)
{
	layui.$.ajax({
		url:url
    ,type: 'post'
    ,data:params
    ,dataType: 'json'
    ,success:function(res){
    	tableext_renderList(cols, tabldid, res.data.rows, pageid, res.data.count, res.data.cond.page, res.data.cond.limit, function(nextpage){
    		params.page = nextpage;
    		tableext_listAction(url, params, cols, tabldid, pageid);
    	});
    }
	});
}

//占
function tablelit_init(func)
{
	myset('tablelit_cols', '');
	myset('tablelit_data', '');
	myset('tablelit_count', '');
	myset('tablelit_cond', '');
	
	listen_init('lit', function(){
		var cols = myget('tablelit_cols');
		var data = myget('tablelit_data');
		var count = myget('tablelit_count');
		var tmp = myget('tablelit_cond');
		var page = tmp.page;
		var limit = tmp.limit;
		tablelit_render('#test-table-simple1', 'test-laypage-demo1', cols, data, page, limit, count);
	}, 2);
	
  //监听行工具事件
  layui.table.on('tool(test-table-simple1)', func);
}
//---------------------------------------------------------------------------------

function tablelit_flush(cond, done)
{
	var index = layer.load();
	
	if(empty(done))
		done = function(){};
	
	if(empty(cond))
	{
		cond = myget('tablelit_cond');
	}
	
	if(empty(cond))
	{
		cond = {};
	}
	
	layui.$.ajax({
		url:myget('tablelit_url')
    ,type: 'post'
    ,data:cond
    ,dataType: 'json'
    ,success:function(res){    	
			myset('tablelit_data', res.data.rows);
			myset('tablelit_count', res.data.count);
			myset('tablelit_cond', res.data.cond);
			tablelit_head();
			listen_set('lit', 1);
			layer.close(index);
			done();
    }
	});
}
//---------------------------------------------------------------------------------

function tablelit_head()
{
	var cols = myget('tablelit_cols');	
	if(!empty(cols))
	{
		listen_set('lit', 0);
	}
	else
	{
		posturl(myget('tablelit_url'), {head:2}, function(res){
			myset('tablelit_cols', res.data);
			listen_set('lit', 0);
		});
	}
}
//---------------------------------------------------------------------------------

function tablelit_render(tableid, pageid, cols, data, page, limit, count)
{
			
			var tmp = {
	      elem: tableid
	      ,data: data
	      ,cellMinWidth: 80
	      ,cols: [cols]
	      ,autoSort:false
	      ,limit:limit
	      ,done:function(res, curr, count){

	      }
	    };
						
	    layui.table.render(tmp);
			
	    //page
	    layui.laypage.render({
	      elem: pageid
	      ,count: count
	      ,curr:page
	      ,limit:limit
	      ,layout: ['prev', 'page', 'next', 'skip']
	      ,jump: function(obj, first){
	        //table_condset('page', obj.curr);
	        
	        if(!first)
	        {
	        	var cond = myget('tablelit_cond');
	        	cond.page = obj.curr;
	        	tablelit_flush(cond);
	        }
	      }
	      
	    });
}

//占
//---------------------------------------------------------------------------------
function timer_listen()
{
	if(empty(timeListen.main))
	{
		timeListen.main = setInterval(function(){ listen('main'); }, 100);
	}
	
	if(empty(timeListen.lit))
	{
		timeListen.lit = setInterval(function(){ listen('lit'); }, 100);
	}
}
//---------------------------------------------------------------------------------
function timer_heart()
{
	if(!empty(timeHeart))
		return;
	
	timeHeart = setInterval(function(){
  	var token = getTokenValue();
  	if(empty(token))
  		return;
  	
	  layui.admin.req({
	    url: api('sysheart')
	    ,done:function(res){
	    	
	    	//封禁用户处理
	    	if(res.data.ukill)
	    	{
	    		layui.view.exit({msg:'用户被封禁,请联系管理员'});
	    		return;
	    	}
	    	
	    	if(!res.data.ulive)
	    	{
	    		layui.view.exit({msg:'登陆已过期，请重新登陆'});
	    		return;
	    	}
	    	
	    }
	  });
  	
	}, 10000);
}

//占
function showBigpicObj(obj)
{
	var $ = layui.$;
	var url = $(obj).attr('bigsrc');
	if(empty(url))
		url = $(obj).attr('src');
	
	showBigpic(url);
}
//---------------------------------------------------------------------------------

function showBigpic(url)
{
	showBigpicIndex = layer.open({
	  type: 1,
	  title: '图片',
	  area: ['520px', '580px'],
	  content: '<img src="'+url+'" style="width:500px;padding:10px;" onclick="layer.close(showBigpicIndex);">'
	});
}
//---------------------------------------------------------------------------------
function selectRender(id, arr, k, v, selected)
{
	if(empty(selected))
		selected = 'SDFLASJDFSALFLKJ365897521SDFHAJK';
	
	var $ = layui.$;
	$('#'+id).html('');
	arr.forEach(item => {
		$('#'+id).append('<option value ="'+item[k]+'" '+(selected==item[k]?'selected':'')+'>'+item[v]+'</option>');
	});
	layui.form.render('select');
}
//---------------------------------------------------------------------------------
function layerOpenReload(str1, str2)
{
	var index = myget('layeropenfn_index');
	layer.close(index);
	setTimeout(function(){
		var params = myget('layeropenfn_params');
		
		if(!empty(str1) && !empty(str2))
			params.content = str_replace(str1, str2, params.content);
		
		var index = layer.open(params);
		myset('layeropenfn_index', index);
	}, 200);
}
//---------------------------------------------------------------------------------

function layerOpenFn(str, title, success, end, windowid, w, h)
{
	if(empty(success))
		success = function(){};
	
	if(empty(end))
		end = function(){};
	
	if(empty(title))
		title = '';
	
	if(empty(windowid))
		windowid = 'openwindow1';
	
	if(empty(w))
	{
		w = window.innerWidth * 0.6;
	}
	
	if(empty(h))
	{
		h = window.innerWidth * 0.4;
	}
	
	var params = {
      type: 1,
      title: title,
      id: windowid,
      shadeClose: false,
      shade: [0.3, '#393D49'],
      maxmin: true, //开启最大化最小化按钮
      area: [w+'px', h+'px'],
      content: str, //注意，如果str是object，那么需要字符拼接。
      skin: 'demo-class'//skin属性可以将layer的标签提取出来，重新定义样式。这个类包含所有按钮样式
      ,btnAlign: 'c'
	  	,success:success
	  	,end:end
  };
	
	var layerIndex = layer.open(params);
  
  myset('layeropenfn_params', params);
  myset('layeropenfn_index', layerIndex);
  
  return layerIndex;
}
//---------------------------------------------------------------------------------
function tplhtml(tplname, viewname, data, func)
{
		var $ = layui.$
		,laytpl = layui.laytpl;
		
		if(empty(func))
			func = function(){};
				
		laytpl($('#'+tplname).html()).render(data, function(html){			
		  $('#'+viewname).html(html);
		  func();
		});
}
//---------------------------------------------------------------------------------
function alert2(msg,func)
{
	if(empty(func))
		func = function(){};
	
  layer.open({
	  title: '提示'
	  ,content: msg
	  ,end:func
	});
}
//---------------------------------------------------------------------------------
function topmarquee_show(v)
{
	var $ = layui.$;
	$('#toptd1').html(v);
	$('#toptd2').show();
}
//---------------------------------------------------------------------------------
function topmarquee_hide()
{
	var $ = layui.$;
	$('#toptd1').html('');
	$('#toptd2').hide();
	var id = $('#toptd2').data('msgid');
  layui.admin.req({
    url: api('msg')
    ,data:{msgid:id}
  });
}
//---------------------------------------------------------------------------------

function formRender(name, data)
{
	var form = layui.form
	,laytpl = layui.laytpl;
	
	form.val(name, data);
	
	for(var i in data)
	{
		laytpl('{{d.'+i+'}}').render(data, function(html){
			$('#tplv_'+i).html(html);
		});
	}
}
//---------------------------------------------------------------------------------
function uitable(id, thead, tdata)
{
	var $ = layui.$;
	
	html = '<table class="layui-table"><thead><tr>';
	
	for(var i in thead)
	{
		html += '<th>'+thead[i]+'</th>';
	}
	
	html += '</tr></thead><tbody>';
	
	for(var k in tdata)
	{
		html += '<tr>';
		for(var j in tdata[k])
		{
			html += '<td>'+tdata[k][j]+'</td>';
		}
		html += '</tr>';
	}
	
	html += '</tbody></table>';
	
	$('#'+id).html(html);
}
//---------------------------------------------------------------------------------

function systemerr(res)
{
  layer.alert(res.msg);
}

//占
//----------------------------------------------------------------------------------

function xhrTransport(url, fileObj, callback)
{
		if(empty(callback))
			callback = function(){};
		
		fileObj.formData = {};
		fileObj.headers = {};
		fileObj.url = url;
		
    var xhr = new XMLHttpRequest();
    var form = new FormData();

		for(var i in fileObj.formData)
		{
			form.append(i, fileObj.formData[i]);
		}
		
    form.append('file', fileObj, fileObj.name);

    xhr.onload = function() {
    	
    	layer.close(layerInXHR);
    	
    	var json = JSON.parse(this.responseText);
    	if(!empty(json) && json.code == 0)
    	{
				callback(json);
			}
			else
			{
			  layer.open({
				  title: '提示'
				  ,content: json.msg
				});
			}
    };

    xhr.onerror = function() {
			layer.close(layerInXHR);
    };

    xhr.onabort = function() {
			
    };
		
		var layerInXHR = layer.load();
		
    xhr.open('POST', fileObj.url, true);

		for(var i in fileObj.headers)
		{
			xhr.setRequestHeader(i, fileObj.headers[i]);
		}

    xhr.send(form);
}
//----------------------------------------------------------------------------------

//占
//---------------------------------------------------------------------------------

function rload(v)
{
	if(empty(v))
		v = location.href;
	
	location.href = v; 
}
//---------------------------------------------------------------------------------

function jump(cc, ca, params)
{
	var url = _url(cc, ca, params);
	jumpUrl(url);
}
//----------------------------------------------------------------------------------
function _url(cc1, ca1, params)
{
	if(empty(cc1))
	{
		cc1 = cc;
	}

	if(empty(ca1))
	{
		ca1 = ca;
	}
	
	var str = '';
	
	if(!empty(params))
	{
		str = '&'+http_build_query(params);
	}
	
	var url = 'index.php?class=' + cc1 + '&func=' + ca1 + str;
	
	return url;
}
//---------------------------------------------------------------------------------
function apibe(cc, ca)
{
	return '/index.php?class=' + cc + '&func=' + ca;
}
//---------------------------------------------------------------------------------
function postapi(cc, ca, data, done, err, silence)
{
	var url = apibe(cc, ca);
	if(empty(url))
	{
		print('url empty:'+k);
		return;
	}
	
	silence ? postsilence(url, data, done, err) : posturl(url, data, done, err);
}
//---------------------------------------------------------------------------------
//没有load圈
function postsilence(url, data, done, err)
{
	if(empty(done))
		done = function(){};

	if(empty(err))
		err = systemerr;

	if(empty(data))
		data = {};

  theajax({
    url: url
    ,type:'post'
    ,data: data
    ,done:function(res){
    	done(res);
    }
    ,isfalse:function(res){
    	err(res);
    }
  });
}

function posturl(url, data, done, err)
{
	if(empty(done))
		done = function(){};

	if(empty(err))
		err = systemerr;
	
	if(empty(data))
		data = {};
	
	var layerInPostUrl = layer.load(1, {time:5000});
	
  theajax({
    url: url
    ,type:'post'
    ,data: data
    ,done:function(res){
    	layer.close(layerInPostUrl);
    	done(res);
    }
    ,isfalse:function(res){
    	layer.close(layerInPostUrl);
    	err(res);
    }
  });
}
//---------------------------------------------------------------------------------
function getlocalWithParams(url, params, done)
{
	//print('getlocalWithParams:'+url);
	
	if(empty(done))
		done = function(){};
	
	layui.$.ajax({
		url:url
    ,type: 'get'
    ,data:params
    ,dataType: 'html'
    ,success:done
	});
}
//---------------------------------------------------------------------------------
//取本地页面
function getlocal(url, done)
{
	getlocalWithParams(url, {}, done);
}
//---------------------------------------------------------------------------------
//取远程页面
function getyc(url, done)
{
	if(empty(done))
		done = function(){};
	
	layui.$.ajax({
		url:url
    ,type: 'get'
    ,dataType: 'html'
    ,success:done
	});
}
//---------------------------------------------------------------------------------
function tlbase(url)
{
	if(!empty(url))
	{
		var cc = url[0];
		var ca1 = url[1];
	}
	else
	{
		var route = layui.router().path;
		var cc = route[0];
		var ca1 = route[1];
	}
	
	var ca2 = str_replace('list', 'one', ca1);
	
	var str1 = '';
	var params = myget('tableurlparams');
	if(!empty(params))
	{
		for(var i in params)
		{
			str1 += '&' + i + '=' + params[i];
		}
	}
		
	return ['/index.php?class='+cc+'&func='+ca1+str1, '/index.php?class='+cc+'&func='+ca2+str1, '/'+cc+'/'+ca1, '/'+cc+'/'+ca2];
}
//---------------------------------------------------------------------------------
function tlist()
{
	var arr = myget('tableurls');
	
	if(empty(arr))
		print('------------------ not at table_init before ------------------');
	
	return arr[0];
}
//---------------------------------------------------------------------------------
function tone()
{
	var arr = myget('tableurls');
	
	if(empty(arr))
		print('------------------ not at table_init before ------------------');
	
	return arr[1];
}
//---------------------------------------------------------------------------------
function tlistlocal()
{
	return tlist();
}
//---------------------------------------------------------------------------------
function tonelocal()
{
	return tone();
}
//---------------------------------------------------------------------------------

function theajax(options)
{
    return $.ajax($.extend({
      type: 'get'
      ,dataType: 'json'
      ,success: function(res){
        
        if(res.code == 0) {
          typeof options.done === 'function' && options.done(res); 
        }
        else {
					typeof options.isfalse === 'function' && options.isfalse(res);
        }
      }
      ,error: function(e, code){
        print(e);
        print(code);
      },
      complete:function(XMLHttpRequest, textStatus){

      }
    }, options));
}

//占
//---------------------------------------------------------------------------------
let wsshand = null;
let wssdebug = true;

function wssinit(url, fn_open, fn_msg, fn_err, fn_close)
{
	if(!empty(wsshand))
		return;
	
	if(empty(fn_open))
	{
		fn_open = function(){};
	}
	
	if(empty(fn_close))
	{
		fn_close = function(){};
	}

	if(empty(fn_msg))
	{
		fn_msg = function(){};
	}

	if(empty(fn_err))
	{
		fn_err = function(){};
	}
	
	wsshand = new WebSocket(url);
	
	wsshand.onopen = function(e){
		if(wssdebug)
		{
			print('--open--');
		}
		
		fn_open(e);
	};
	
	wsshand.onmessage = function(object){
		
		if(wssdebug)
		{
			print('--get--');
			print(object.data);
		}
		
		wssget(object, fn_msg)
	};
	
	wsshand.onerror = function(e){
		
		if(wssdebug)
		{
			print('--err--');
			print(e);
		}
		
		wssclose();
		fn_err(e);
	};
	
	wsshand.onclose = function(e){
		if(wssdebug)
		{
			print('--close--');
		}
		
		fn_close(e);
	};
}
//---------------------------------------------------------------------------------
function wssget(object, func)
{
	if(substr(object.data, 0, 1) !== '{')
	{
		print('--!!!fail--');
		return;
	}
	
	var data = JSON.parse(object.data);
	func(data);
}
//---------------------------------------------------------------------------------

function wsssend(str)
{
	if(is_object(str))
		str = jsone(str);
	
	if(wssdebug)
	{
		print('--send--');
		print(str);
	}
	
	wsshand.send(str);
}
//---------------------------------------------------------------------------------

function wssclose()
{
	wsshand.close();
	wsshand = null;
}

function array_change_key_case(arr, low)
{
	if(typeof(arr) != 'object')
	{
		alert('array_change_key_case false!');
	}
	
	var r = {};
	var j = 0;
	for(var i in arr)
	{
		j = low ? i.toLowerCase() : i.toUpperCase();
		r[j] = arr[i];
	}
	
	return r;
}
//----------------------------------------------------------------------------------
function array_chunk(arr, size)
{
	var r = [];
	var j = 0;
	var k = {};
	
	for(var i in arr)
	{
		if(j >= size)
		{
			j = 0;
			r.push(k);
			k = {};
		}
		
		
		k[i] = arr[i];
		j++;
	}
	
	if(array_size(k) > 0)
	{
		r.push(k);
	}
	
	return r;
}
//----------------------------------------------------------------------------------
function array_combine(arr1, arr2)
{
	var k = {};

	for(var i in arr1)
	{
		var a = arr1[i];
		var b = arr2[i];
		k[a] = b;
	}
	
	return k;
}
//----------------------------------------------------------------------------------
function array_diff(arr1, arr2)
{
	var r = {};
	
	for(var i in arr1)
	{
		var isin = false;
		var v = arr1[i];
		
		for(var j in arr2)
		{
			if(arr2[j] == v)
			{
				isin = true;
				break;
			}
		}
		
		if(isin == false)
		{
			r[i] = v;
		}
	}
	
	return r;
}
//----------------------------------------------------------------------------------
function array_flip(arr)
{
	var r = {};
	
	for(var i in arr)
	{
		var v = arr[i];
		r[v] = i;
	}
	
	return r;
}
//----------------------------------------------------------------------------------
function array_intersect(arr1, arr2)
{
	var r = {};
	
	for(var i in arr1)
	{
		var isin = false;
		var v = arr1[i];
		
		for(var j in arr2)
		{
			if(arr2[j] == v)
			{
				isin = true;
				break;
			}
		}
		
		if(isin == true)
		{
			r[i] = v;
		}
	}
	
	return r;
}
//----------------------------------------------------------------------------------
function array_keys(arr)
{
	var r = [];
	
	for(var i in arr)
	{
		r.push(i);
	}
	
	return r;
}
//----------------------------------------------------------------------------------
function array_values(arr)
{
	var r = [];
	
	for(var i in arr)
	{
		r.push(arr[i]);
	}
	
	return r;
}
//----------------------------------------------------------------------------------
function array_map(arr, func)
{
	var r = {};
	
	for(var i in arr)
	{
		r[i] = func(arr[i]);
	}
	
	return r;
}
//----------------------------------------------------------------------------------
function array_merge(arr1, arr2)
{
	var arr = [];
	
	for(var i in arr1)
	{		
		if(is_numeric(i))
		{
			arr.push(arr1[i]);
		}
		else
		{
			arr[i] = arr1[i];
		}
	}

	for(var i in arr2)
	{
		if(is_numeric(i))
		{
			arr.push(arr2[i]);
		}
		else
		{
			arr[i] = arr2[i];
		}
	}

	return arr;
}
//----------------------------------------------------------------------------------
function array_pop(arr)
{
	return arr.pop();
}
//----------------------------------------------------------------------------------
function array_push(arr, v)
{
	return arr.push(v);
}
//----------------------------------------------------------------------------------
function array_shift(arr)
{
	return arr.shift();
}
//----------------------------------------------------------------------------------
function array_unshift(arr, v)
{
	return arr.unshift(v);
}
//----------------------------------------------------------------------------------
function array_rand(arr)
{
	var max = array_size(arr);
	var j = rand(1, max);
		
	for(var i in arr)
	{
		j--;

		if(j == 0)
		{
			return i;
		}
	}
	
	return i;
}
//----------------------------------------------------------------------------------
function array_reverse(arr)
{
	var r = [];
	var r2 = {};
	
	for(var i in arr)
	{
		r.unshift(i);
	}
	
	for(var j in r)
	{
		var k = r[j];
		r2[k] = arr[k];
	}
	
	return r2;
}
//----------------------------------------------------------------------------------
function array_search(arr, v)
{	
	for(var i in arr)
	{
		if(arr[i] == v)
		{
			return i;
		}
	}
	
	return false;
}
//----------------------------------------------------------------------------------
function array_sum(arr)
{
	var j = 0;
	
	for(var i in arr)
	{
		j = j + arr[i];
	}
	
	return j;
}
//----------------------------------------------------------------------------------
function array_unique(arr)
{
	var r = [];
	
	for(var i in arr)
	{
		if(!in_array(arr[i], r))
		{
			r.push(arr[i]);
		}
	}
	
	return r;
}
//----------------------------------------------------------------------------------
function array_length(arr)
{
	return array_size(arr);
}
//----------------------------------------------------------------------------------
function array_size(arr)
{
	var j = 0;
	for(var i in arr)
	{
		j++;
	}
	
	return j;
}
//----------------------------------------------------------------------------------
function in_array(v, array)
{
	var b = JSON.stringify(v);
	
	for(var i in array)
	{
		var a = JSON.stringify(array[i]);
		
		if(a == b)
		{
			return true;
		}
	}
	
	return false;
}
//----------------------------------------------------------------------------------
function array_join(v, c)
{
	if(typeof(c) == 'object')
	{
		c = c.join('');
	}
	
	for(var i in v)
	{
		v[i] = v[i] + c
	}
	
	return v;
}
//----------------------------------------------------------------------------------

function array_pad(arr, l, v)
{
	if(arr.length < l)
	{
		l = l - arr.length;
		for(i=0;i<l;i++)
		{
			arr.push(v);
		}
	}
	
	return arr;
}
//----------------------------------------------------------------------------------
function array_currentIndex(l, curr, v)
{
	var r = v ? curr+1 : curr-1;
	if(r < 0)
	{
		r = l - 1;
	}
	else if(r >= l)
	{
		r = 0;
	}
	
	return r;
}
//----------------------------------------------------------------------------------
function array_shuffle(arr)
{
	arr.sort();
	
	var l = arr.length;
	var i = 0;
	var n = [];
		
	while(true)
	{
		var j = rand(0, l-1);
				
		var v = arr[j];
		
		if(!in_array(v, n))
		{
			n.push(v);
			
			if(n.length >= l)
			{
				return n;
			}
		}
	}
}
//----------------------------------------------------------------------------------
function array_slice(arr, offset, len)
{
	return arr.slice(offset, offset + len);
}

//----------------------------------------------------------------------------------
function basename(f)
{
	f = str_replace('\\', '/', f);
	var pos = strrpos(f, '/');
	
	if(pos === false)
	{
		return f;
	}
	
	return substr(f, pos + 1);
}
//----------------------------------------------------------------------------------

function dirname(f)
{
	f = str_replace('\\', '/', f);
	var pos = strrpos(f, '/');
	
	if(pos === false)
	{
		return f;
	}
	
	return substr(f, 0, pos);
}
//----------------------------------------------------------------------------------
function copy(f)
{
	fs_get(f, function(str){
		fs_put(str);
	})
}
//----------------------------------------------------------------------------------
function fs_get(f, func)
{
	file_get_contents(f, func);
}
//----------------------------------------------------------------------------------

function fs_put(str)
{
	file_put_contents(str);
}
//----------------------------------------------------------------------------------
//fˇtype=file
//˽¾ފƢase¹�/¾­phpµâase_decodeֽ³£¸´ԭ
function file_get_contents(f, func)
{
		//var blob = f[0].files[0];
		var blob = new Blob([f[0].files[0]]);
		var reader = new FileReader();
		reader.onload = function(e)
		{
			var str = this.result;
			str = strstr(str, ',');
			str = substr(str, 1);
			//str = based(str);
			func(str);
		};
		reader.readAsDataURL(blob);
}
//----------------------------------------------------------------------------------
function file_put_contents(str)
{
	var blob = new Blob([str], {"type":"application/octet-stream"});
	var url = URL.createObjectURL(blob);
	var a = $('<a href="'+url+'">click</a>');
	var e = document.createEvent('MouseEvents');
	e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
	a[0].dispatchEvent(e);
}
//----------------------------------------------------------------------------------


//----------------------------------------------------------------------------------
function isIE()
{
	return /msie/.test(navigator.userAgent.toLowerCase());
}

function isFF()
{
	return /firefox/.test(navigator.userAgent.toLowerCase());
}

function isGG()
{
	return /webkit/.test(navigator.userAgent.toLowerCase());
}

function is_array(v)
{
	return Object.prototype.toString.call(v) === '[object Array]';
}

function is_string(v)
{
	return typeof(v) == 'string';
}

function is_int(v)
{
	return typeof(v) == 'number';
}

function is_function(v)
{
	return typeof(v) == 'function';
}

function is_numeric(v)
{
	if(is_int(v)){return true;}
	if(is_string(v))
	{
		var re = new RegExp("[^0-9]");
		return re.test(v) == false;
	}
	return false;
}

function is_null(v)
{
	return typeof(v) == 'undefined' || v == null;
}

function is_object(v)
{
	var type = typeof(v);
	
	return type != 'undefined' && type != 'number' && type != 'string' && type != 'boolean' && type != 'function';
}

function lenObj(obj)
{
	var j = 0;
	
	for(var i in obj)
	{
		j++;
	}
	
	return j;
}

function is_empty(v)
{
	if(is_null(v)) return true;
	if(v == 0) return true;
	if(v === '') return true;
	if(v === '0') return true;
	if(v === []) return true;
	if(v === {}) return true;
	if((is_object(v) || is_array(v)) && lenObj(v) == 0)	return true;
	if(!isset(v)) return true;
	return false;
}

//----------------------------------------------------------------------------------
//以秒论，getTime是毫秒
function sleep(time)
{
	usleep(time * 1000);
}
//----------------------------------------------------------------------------------
//以毫秒
function usleep(time)
{
	var now = new Date(); 
	var t = now.getTime() + time;
	while(true)
	{
		now = new Date();
		if(now.getTime() > t)
		{
			return;
		}
	}
}
//----------------------------------------------------------------------------------
function pageReload()
{
	window.location.reload();
}
//----------------------------------------------------------------------------------
function pageJump(url)
{
	window.location.href = url;
}
//----------------------------------------------------------------------------------
function jumpUrl(url)
{
	pageJump(url);
}
function jumpPage(url)
{
	pageJump(url);
}
//----------------------------------------------------------------------------------
function empty(v)
{
	return is_empty(v);
	//if(v === [] || v === {}) return false;
	//return false == new Boolean(v);
}
//----------------------------------------------------------------------------------
function _split(s, v)
{
	return explode(s, v);
}
//----------------------------------------------------------------------------------
function rand(min, max)
{
	var r = Math.random();
	return Math.round((max - min) * r) + min;
}
//----------------------------------------------------------------------------------
function isset(v)
{
	if(typeof(v) == 'undefined') return false;
	if(v == 'undefined') return false;
	
	return true;
}
//----------------------------------------------------------------------------------
function time()
{
	var _d = new Date();
	var time = _d.getTime();
	time = new String(time);
	time = substr(time, 0, 10);
	time = parseInt(time);
	return time;
}
//----------------------------------------------------------------------------------
function date(str, time)
{
	time = new String(time) + '000';
	time = parseInt(time);
	var _ = new Date(time);
	var Y = _.getUTCFullYear();
	
	var N = _.getDay();
	N = N == 7 ? 0 : N;
	
	var n = _.getMonth() + 1;
	var m = n < 10 ? ('0'+n) : n;
	
	var j = _.getDate();
	var d = j < 10 ? ('0'+j) : j;
	
	var G = _.getHours();
	var H = G < 10 ? ('0'+G) : G;
	var g = G > 12 ? (G - 12) : G;
	var h = h < 10 ? ('0'+h) : h;
	
	var i = _.getMinutes();
	i = i < 10 ? ('0'+i) : i;
	
	var s = _.getSeconds();
	s = s < 10 ? ('0'+s) : s;
	
	str = str_replace('s', s, str);
	str = str_replace('i', i, str);
	str = str_replace('H', H, str);
	str = str_replace('G', G, str);
	str = str_replace('d', d, str);
	str = str_replace('j', j, str);
	str = str_replace('m', m, str);
	str = str_replace('n', n, str);
	str = str_replace('N', N, str);
	str = str_replace('Y', Y, str);
	str = str_replace('g', g, str);
	str = str_replace('h', h, str);
	return str;
}
//----------------------------------------------------------------------------------
function de(str)
{
	return unescape(str);
}
//----------------------------------------------------------------------------------
function en(str)
{
	return escape(str);
}
//----------------------------------------------------------------------------------
function based(str)
{
	return window.atob(str);
}
//----------------------------------------------------------------------------------
function basee(str)
{
	return window.btoa(str);
}
//----------------------------------------------------------------------------------
function randmd5()
{
	var s = 'qwertyuioplkjhgfdsazxcvbnm0123698745';
	var l = s.length - 1;
	var str = '';
	
	for(i=0;i<32;i++)
	{
		j = rand(0, l);
		str += s.charAt(j);
	}
	
	return str;
}

//----------------------------------------------------------------------------------
function http_build_query( arr )
{
	var array = [];
	
	for(var i in arr)
	{
		array.push(i+'='+arr[i]);
	}
	
	return array.join( "&" );
}
//----------------------------------------------------------------------------------
function _log(str)
{
	console.log(str);
}
//----------------------------------------------------------------------------------
function print(str)
{
	console.log(str);
}
//----------------------------------------------------------------------------------
function _alert(str)
{
	alert(str);
}
//----------------------------------------------------------------------------------

function int(v)
{
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
}
//----------------------------------------------------------------------------------

function look_all(func, yes, time, arr)
{
			var f = function(){
				if(func())
				{
					yes(arr);
				}
			};
			var int = setInterval(f, time);
			return int;
};
//----------------------------------------------------------------------------------
function look_one(func, yes, time, arr)
{
			var f = function(){
				if(func())
				{
					clearInterval(int);
					yes(arr);
				}
			};
			var int = setInterval(f, time);
			return int;
}
//----------------------------------------------------------------------------------
//urlreplace(url, b, f)把xxx.php?a=c&b=d换成xxx.php?a=c&b=f
function urlreplace(url, k, v)
{
	var p = "([?&])"+k+"\=[a-zA-Z0-9_]+";
	var url1 = preg_replace([p,'g'], "$1"+k+"="+v, url);
	
	if(url1 == url)
		url1 = url + '&' + k + '=' + v;
	
	return url1;
}
//----------------------------------------------------------------------------------
function print_r(o, first)
{
	if(first){$.mem.init('t');}
	
	$.each(o, function(i, n){
		var str = $.mem.get('t');
		if(typeof(n) == 'object')
		{
			$.mem.set('t', str + "\n[" + i + "]");
			print_r(n, 0);
		}
		else
		{
			$.mem.set('t', str + "\n" + i + ": " + n);
		}
	});
	
	if(first){alert($.mem.get('t'))};
}
//----------------------------------------------------------------------------------
function foreach(arr, func)
{
	var l = arr.length;
	
	for(var k=0;k < l;k++)
	{
		arr = func(arr, k, arr[k]);
	}

	return arr;
}
//----------------------------------------------------------------------------------
function cnNumber(v)
{
	if(v == 0) return '零';
	if(v == 1) return '一';
	if(v == 2) return '二';
	if(v == 3) return '三';
	if(v == 4) return '四';
	if(v == 5) return '五';
	if(v == 6) return '六';
	if(v == 7) return '七';
	if(v == 8) return '八';
	if(v == 9) return '九';
	if(v == 10) return '拾';
}
//----------------------------------------------------------------------------------


function chr(num)
{
	return String.fromCharCode(num);
}
//----------------------------------------------------------------------------------
function ord(str)
{
	return str.charCodeAt(0);
}
//----------------------------------------------------------------------------------
function ords(str)
{
	var arr = str_split(str);
	var n = [];
	for(var i in arr)
	{
		n.push(ord(arr[i]));
	}
	
	return n;
}
//----------------------------------------------------------------------------------
function chunk_split(str, len)
{
	var tmp = str_split(str, len);
	
	return join("\r\n", tmp);
}
//----------------------------------------------------------------------------------
function echo(str)
{
	$('body').append("<div class=yadcssecho>"+str+"</div>");
}
//----------------------------------------------------------------------------------
function parse_str(str)
{	
	str = str_replace('&', '","', str);
	str = str_replace('=', '":"', str);
	str = '{"' + str + '"}';
	eval('var tmp = ' + str + ';');
	return tmp;
}
//----------------------------------------------------------------------------------
function ltrim(str, s)
{
	if(!is_string(str))
	{
		str = '';
	}
	
	if(str == '')
	{
		return '';
	}
	
	if(str.indexOf(s) === 0)
	{
		var tmp = substr(str, s.length);
		str = ltrim(tmp, s);
	}
	
	return str;
}
//----------------------------------------------------------------------------------
function rtrim(str, s)
{
	if(!is_string(str))
	{
		str = '';
	}
	
	if(str == '')
	{
		return '';
	}
	
	var l1 = str.length;
	var l2 = s.length;
	var i = str.lastIndexOf(s);

	if(i === (l1-l2))
	{
		var tmp = substr(str, 0, i);
		str = rtrim(tmp, s);
	}
	
	return str;
}
//----------------------------------------------------------------------------------
function trim(str, s)
{
	str = ltrim(str, s);
	str = rtrim(str, s);
	return str;
}
//----------------------------------------------------------------------------------
function explode(s, v)
{
	if(v == '')
	{
		return [];
	}
	
	return v.split(s);
}
//----------------------------------------------------------------------------------
function str_pad(str, l, v, type)
{
	if(str.length >= l)
	{
		return str;
	}
	
	if(type == 'l')
	{
		str = v + str;
	}
	else if(type == 'r')
	{
		str = str + v;
	}
	else
	{
		str = v + str + v;
	}
	
	if(str.length < l)
	{
		str = str_pad(str, l, v, type);
	}
	
	if(str.length > l && type == 'l')
	{
		return substr(str, str.length - l);
	}
	
	return substr(str, 0, l);
}

//----------------------------------------------------------------------------------
function str_repeat(v, n)
{
	var s = '';
	for(i=0;i<n;i++)
	{
		s += v;
	}
	
	return s;
}
//----------------------------------------------------------------------------------
function str_shuffle(str)
{
	var arr = str_split(str, 1);
	arr = array_shuffle(arr);
	return join('', arr);
}
//----------------------------------------------------------------------------------
function str_split(str, len)
{
	if(!is_numeric(len))
	{
		len = 1;
	}
	
	var sep = Math.ceil(str.length / len);
	var i = 0;
	var tmp = [];
	
	for(i; i < sep; i++)
	{
		var s = substr(str, i * len, len);
		tmp.push(s);
	}
	
	return tmp;
}
//----------------------------------------------------------------------------------
function strstr(str, f)
{
	var i = strpos(str, f);
	return substr(str, i);
}

function strrstr(str, f)
{
	var i = strrpos(str, f);
	return substr(str, i);
}
//----------------------------------------------------------------------------------
function strspn(str, chrs)
{
	var arr = str_split(str, 1);
	
	for(var i in arr)
	{
		if(strpos(chrs, arr[i]) === false)
		{
			return i;
		}
	}
}
//----------------------------------------------------------------------------------
function strcspn(str, chrs)
{
	var arr = str_split(str, 1);
	
	for(var i in arr)
	{
		if(strpos(chrs, arr[i]) !== false)
		{
			return i;
		}
	}
}
//----------------------------------------------------------------------------------
function strip_tags(str)
{
	return preg_replace(/<[^>]*>/g, '', str);
}
//----------------------------------------------------------------------------------
function strlen(str)
{
	return str.length;
}
//----------------------------------------------------------------------------------
function strnatcmp(str1, str2)
{
	return str1 === str2;
}
//----------------------------------------------------------------------------------
function strpbrk(str, chrs)
{
	var arr = str_split(str, 1);
	
	for(var i in arr)
	{
		if(strpos(chrs, arr[i]) !== false)
		{
			return substr(str, i);
		}
	}
}
//----------------------------------------------------------------------------------
function strpos(str, f)
{
	f = preg_quote(f);
	var re = new RegExp(f);
	var index = str.search(re);
	if(index == -1) return false;
	return index;
}
//----------------------------------------------------------------------------------
function str_count(str, s)
{
	var arr = explode(str, s);
	var l = arr.length - 1;
	return l > 0 ? l : 0;
}
//----------------------------------------------------------------------------------
function substr(v, s, l)
{
	return typeof(l) == 'undefined' ? v.substr(s) : v.substr(s, l);
}
//----------------------------------------------------------------------------------
function str_replace_one(s, r, v)
{
	return v.replace(s, r);
}
//----------------------------------------------------------------------------------

function strrev(s)
{
	var l = s.length - 1;
	var c = '';
	for(i = 0;l>=i;l--)
	{
		c = c + s.charAt(l);
	}
	return c;
}
//----------------------------------------------------------------------------------
function strrpos(s, f)
{
	s = strrev(s);
	var i = strpos(s, f);
	return strlen(s) - 1 - i;
}
//----------------------------------------------------------------------------------

function str_replace(s, r, v)
{
	s = preg_quote(s);
	s = new RegExp(s, 'g');
	return v.replace(s, r);
}
//----------------------------------------------------------------------------------
function join(v, arr)
{
	return arr.join(v);
}
//----------------------------------------------------------------------------------
function strtolower(str)
{
	return str.toLowerCase();
}
//----------------------------------------------------------------------------------
function strtoupper(str)
{
	return str.toUpperCase();
}

function substr_replace(s, re, start, l)
{
	return substr(s, 0, start) + re + substr(s, start + l);
}
//----------------------------------------------------------------------------------
function preg_match_all(p, str)
{
	str.match(p);
	return [
		RegExp.$1,
		RegExp.$2,
		RegExp.$3,
		RegExp.$4,
		RegExp.$5,
		RegExp.$6,
		RegExp.$7,
		RegExp.$8,
		RegExp.$9
	];
}
//----------------------------------------------------------------------------------
function preg_replace(p, re, str)
{
	if(is_array(p))
		p = new RegExp(p[0], p[1]);
	else if(is_string(p))
		p = new RegExp(p, 'g');
	
	return str.replace(p, re);
}
//----------------------------------------------------------------------------------
function preg_quote(str)
{
	var arr = str_split(str, 1);
		
	for(var i in arr)
	{
		if(arr[i] == '.')
		{
			arr[i] = '\\.';
		}
		else if(arr[i] == '\\')
		{
			arr[i] = '\\\\';
		}
		else if(arr[i] == '^')
		{
			arr[i] = '\\^';
		}
		else if(arr[i] == '(')
		{
			arr[i] = '\\(';
		}
		else if(arr[i] == ')')
		{
			arr[i] = '\\)';
		}
		else if(arr[i] == '[')
		{
			arr[i] = '\\[';
		}
		else if(arr[i] == ']')
		{
			arr[i] = '\\]';
		}
		else if(arr[i] == '$')
		{
			arr[i] = '\\$';
		}
		else if(arr[i] == '*')
		{
			arr[i] = '\\*';
		}
		else if(arr[i] == '+')
		{
			arr[i] = '\\+';
		}
		else if(arr[i] == '?')
		{
			arr[i] = '\\?';
		}
		else if(arr[i] == '|')
		{
			arr[i] = '\\|';
		}
		else if(arr[i] == '{')
		{
			arr[i] = '\\{';
		}
		else if(arr[i] == '}')
		{
			arr[i] = '\\}';
		}
		else if(arr[i] == '=')
		{
			arr[i] = '\\=';
		}
		else if(arr[i] == '!')
		{
			arr[i] = '\\!';
		}
		else if(arr[i] == '-')
		{
			arr[i] = '\\-';
		}
		else if(arr[i] == ':')
		{
			arr[i] = '\\:';
		}
		
	}
		
	return join('', arr);
}

function ceil(v)
{
	v = parseInt(v);
	return Math.ceil(v);
}

function floor(v)
{
	v = parseInt(v);
	return Math.floor(v);
}

